package net.findkrypton.entity;

import java.awt.Point;

import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import net.findkrypton.enume.LoopStateEnum;
import net.jumpengine.bean.TextState;
import net.jumpengine.inter.IAction;
import net.jumpengine.inter.IClick;
import net.jumpengine.po.Border;
import net.jumpengine.util.UDraw;

public class TextStateBase extends TextState implements IClick{
	private int r,cx,cy,cwidth,cheight;
	private boolean change=true;
	private Point p;
	private IAction action;

	public TextStateBase(int x, int y, int width, int height,String text,Border border) {
		this(x, y, width, height, 0, text, border);
	}
	
	public TextStateBase(int x, int y, int width, int height, int index,String text,Border border) {
		super(x, y, width, height, index, text, border);
		color=Color.BROWN;
		backColor=Color.ORANGE;
	}

	@Override
	public void draw(GraphicsContext g) {
		r=border==null?0:border.getRadius();
		if(border!=null)g.setFill(state==LoopStateEnum.nomal?border.getColor():border.getMoveIn());
		if(r==0) {
			g.fillRect(x, y, width, height);
		}else {
			g.fillRoundRect(x, y, width, height, r, r);
		}
		g.setFill(backColor);
		
		if(change) {
			cx=x+(border==null?0:border.getLeft());
			cy=y+(border==null?0:border.getTop());
			cwidth= width-(border==null?0:border.getRight())-(border==null?0:border.getLeft());
			cheight=height-(border==null?0:border.getBottom())-(border==null?0:border.getTop());
		}
		
		if(r==0) {
			g.fillRect(cx,cy,cwidth,cheight);
		}else {
			g.fillRoundRect(cx,cy, cwidth, cheight, r, r);
		}
		
		if(change) {
			p = UDraw.centerString(text, font, cwidth, cheight);
			change=false;
		}
		g.setFill(color);
		g.setFont(font);
		if(font.getStyle().indexOf("Bold")>=0) {
			UDraw.boldString(text, p.x+cx, p.y+cy, g);
		}else {
			g.fillText(text, p.x+cx, p.y+cy);
		}
	}

	public LoopStateEnum getState() {
		return (LoopStateEnum) state;
	}
	public void setState(LoopStateEnum state) {
		this.state = state;
	}

	public String getText() {
		return text;
	}
	public void setText(String text) {
		change=true;
		this.text = text;
	}

	public Border getBorder() {
		return border;
	}
	public void setBorder(Border border) {
		change=true;
		this.border = border;
	}

	public Font getFont() {
		return font;
	}
	public void setFont(Font font) {
		change=true;
		this.font = font;
	}

	public Color getColor() {
		return color;
	}
	public void setColor(Color color) {
		this.color = color;
	}

	public Color getBackColor() {
		return backColor;
	}
	public void setBackColor(Color backColor) {
		this.backColor = backColor;
	}

	public IAction getAction() {
		return action;
	}
	public void setAction(IAction action) {
		this.action = action;
	}
}
